﻿using DevExpress.XtraEditors.DXErrorProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetExtensions.AspNet.DevExpress.Services
{
    public interface IValidationService
    {
        void SetRules(System.Windows.Forms.Control control, params ValidationRule[] rules);
        bool Validate();
    }

    public class ValidationService : IValidationService
    {
        private readonly DXValidationProvider validationProvider;
        public ValidationService(DXValidationProvider validationProvider)
        {
            this.validationProvider = validationProvider;
        }

        public void SetRules(System.Windows.Forms.Control control, params ValidationRule[] rules)
        {
            if (validationProvider == null) return;

            foreach (var rule in rules)
            {
                validationProvider.SetValidationRule(control, rule);
            }
        }

        public bool Validate()
        {
            if (validationProvider == null) return true;
            if (validationProvider.ValidationMode == ValidationMode.Auto) return true;
            return validationProvider.Validate();
        }
    }
}
